﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TAB05RP
    {

        public TAB05RP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MESSAGES += "AB05RP;";
            InitializeComponent();
            POSTYP = "AM ";
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                IsCanChg = false;
            }
            else
            {
                IsCanChg = true;
            }
        }

        #region====WorkFlow========
        MessageInfo Lastmsg
        {
            get
            {
                return TMV.Lastmsg;
            }
            set
            {
                TMV.Lastmsg = value;
            }
        }
         
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            IsBusy = true;
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {

                case "AB05RP":
                    if (Lastmsg != null)
                    {
                        //MessageBox.Show("请先完成前一个订单创建");
                        //return;
                    }
                    RULEKEY = "";//先装载规则
                    RULEKEY = msg.ORDER;
                    Lastmsg = msg;
                    ARTST = msg.WAKZ;
                    ART = msg.ART;
                    VAR = msg.VAR;
                    FARBE = msg.FARBE;
                    FINISH = msg.BESCHICH;
                    DESS = msg.DESS;
                    COLORIT = msg.COLORIT;
                    FSIZE = msg.GROE;
                    DESS = msg.DESS;
                    COLORIT = msg.COLORIT;
                    FSIZE = msg.GROE;
                    FINISH = msg.BESCHICH;
                    PRART = ARTST + ART + VAR + FARBE + FINISH + DESS + COLORIT;
                    PM["ORDER"] = msg.ORDER;
                    PM["WAKZ"] = msg.WAKZ;
                    PM["ART"] = msg.ART;
                    PM["VAR"] = msg.VAR;
                    PM["FARBE"] = msg.FARBE;
                    PM["BESCHICH"] = msg.BESCHICH.PadRight(10);
                    PM["DESS"] = msg.DESS.PadRight(10);
                    PM["COLORIT"] = msg.COLORIT.PadRight(10);
                    PM["GROE"] = msg.GROE.PadRight(7);
                    PM["MAKE2ORD"] = false;
                    PM["REWEIL"] = msg.P2;
                    IsOK = false;
                    TMV.AB05RP(PM, ref IsOK);
                    IsABPCreated = IsOK;
                    if (IsABPCreated)
                    {
                        ORDERNUM = msg.ORDER;
                    }
                    break;
            }

            IsBusy = false;
        }
        #endregion

        public override void OnAutoChg(bool auto)
        {
            if (!auto)
                return;
            if (dwRule == null)
            {
            }
            base.OnAutoChg(auto);
        }

        private void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            IsABPCreated = false;
            if (Lastmsg == null)
            {
                Lastmsg = new MessageInfo();
                Lastmsg.ORDER = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();
                Lastmsg.ART = dwAB["ABPART"].ToString();
                Lastmsg.VAR = dwAB["ABPVAR"].ToString();
                Lastmsg.FARBE = dwAB["ABPFARBE"].ToString();
                Lastmsg.BESCHICH = dwAB["ABPBESCHICH"].ToString();
                Lastmsg.DESS = dwAB["ABPDESS"].ToString();
                Lastmsg.COLORIT = dwAB["ABPCOLORIT"].ToString();
                Lastmsg.GROE = dwAB["ABPGROE"].ToString();
                Lastmsg.P2 = dwAB["ABPREWEIL"].ToString();
            }
            PM["ORDER"] = Lastmsg.ORDER;
            PM["WAKZ"] = Lastmsg.WAKZ;
            PM["ART"] = Lastmsg.ART;
            PM["VAR"] = Lastmsg.VAR;
            PM["FARBE"] = Lastmsg.FARBE.PadRight(10);
            PM["BESCHICH"] = Lastmsg.BESCHICH.PadRight(10);
            PM["DESS"] = Lastmsg.DESS.PadRight(10);
            PM["COLORIT"] = Lastmsg.COLORIT.PadRight(10);
            PM["GROE"] = Lastmsg.GROE.PadRight(7);
            PM["MAKE2ORD"] = false;
            PM["REWEIL"] = Lastmsg.P2;
            IsOK = false;
            TMV.AB05RP(PM, ref  IsOK);
            IsABPCreated = IsOK;
            if (IsABPCreated && dwAB != null)
            {
                string key = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                ORDERNUM = "";
                ORDERNUM = key;
            }
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            KRS = EXAUF.Substring(1, 2);
            LFD = EXAUF.Substring(3, 6);
            POS = EXAUF.Substring(9, 3);
            ORDERNUM = KRS + LFD + POS + POSTYP;
            base.OnEXAUFChg(vlaue);
        }
        public override void OnFLOWChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FLOW == "")
                return;
            if (dwRule == null)
                return;
            TMV.ABPANRKR = FLOW.Substring(0, 2);
            TMV.ABPALFDNR = FLOW.Substring(2, 6);
            TMV.UABP19(dwMaster);
            PM["ORDERNUM"] = TMV.ABPAUFKRS + TMV.ABPAUFLFD + TMV.ABPAUFPOS + TMV.ABPPOSTYP + "V";
            PM["AUFTYP"] = "AB05";
            TMV.VABF.ABFNEW(PM);
            TMV.VPR.UpdateICC_PRULES(dwRule, 1);
            base.OnFLOWChg(value);
        }

    }
    partial class VABPOS
    {
        public void AB05RP(Dictionary<string, object> pm, ref bool IsABPCreated)
        {
            try
            { 
                if (dwWS == null)
                    return;
                if (dwRule["UNDES"].ToString().Trim().Length < 8)
                    return;
                if (PRLO.Length < 2)
                    return;
                if (PRLB.Length < 2)
                    return;
                
                
                PM["VKAUF"] = dwRule["P4"];
                PM["KRS"] = KRS;
                APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
                APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
                if (APZALFDNR.Trim().Length < 6)
                {
                    throw new Exception("请选择流程");
                }
                #region==============规则================ 
                QTYMAT = 0;
                double _R01 = 0;
                double _R02 = 0;
                double _F01 = 0;
                double _F02 = 0;
                double _LEADTIME = 0;
               
                if (dwRule != null)
                {
                    _R01 = R01 / 10000;//整理缩率
                    _R02 = R02 / 100;//零次布
                    _F02 = F02;//成品可用  
                    _LEADTIME = Convert.ToDouble(LEADTIME);//生产周期
                    INMODE = dwRule["P8"].ToString();
                    OUTMODE = dwRule["P9"].ToString();
                }

                if (F02 > 0)
                {
                    QTYEND = QTYEND - F02;
                }

                string STARTD = Helpers.Helper.GetDateAddDays(DELDATE, Convert.ToInt16(0 - _LEADTIME));
                if (STARTD.Trim() == "") STARTD = "0";

                double WARSTDLG = Convert.ToDouble(dwWS["WARSTDLG"].ToString());
                double WALAEAEND = System.Convert.ToDouble(dwWS["WALAEAEND"].ToString());
                string UM = dwWS["WAUEBM"].ToString();
                double WAFBRTL = System.Convert.ToDouble(dwWS["WAFBRTL"].ToString());
                double WARGRLFD = System.Convert.ToDouble(dwWS["WARGRLFD"].ToString());
                double WAFGRLFD = System.Convert.ToDouble(dwWS["WAFGRLFD"].ToString());
                double WAFSTDLG = System.Convert.ToDouble(dwWS["WAFSTDLG"].ToString());
                double WARWBRT = System.Convert.ToDouble(dwWS["WARWBRT"].ToString());
                if (WAFSTDLG == 0)
                {
                    WAFSTDLG = 100000;
                }

                QTYMAT = Math.Ceiling(QTYEND * (1 + R02) / (1 - R01));
                #endregion
                #region=======dwData==========
                DataRow dwABP = TBMain.NewRow();
                SetUpNewRow(ref dwABP, null);
                ShareedABP(dwABP, pm);
                PM["REWEIL"] = pm["REWEIL"];
                dwABP["ABPREWEIL"] = pm["REWEIL"];
                dwABP["ABPAENDZL"] = 6;
                dwABP["ABPMANDANT"] = app.Mandant;
                dwABP["ABPWAKZ"] = "F";
                dwABP["ABPAUFART"] = "VA";
                dwABP["ABPSERIE"] = " ";
                dwABP["ABPSME"] = 2;
                dwABP["ABPWAART"] = "   ";
                dwABP["ABPERSTBE"] = "E";
                dwABP["ABPSZUSATZKZ1"] = "F";
                dwABP["ABPMGV"] = "10";
                dwABP["ABPKDDAT"] = DELDATE;

                dwABP["ABPSTDLAE"] = Convert.ToDecimal(dwWS["WARSTDLG"].ToString()) / 10;
                dwABP["ABPMGV"] = 10;
                dwABP["ABPMGB"] = 9999999;
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPZLB"] = PRLB;
                dwABP["ABPLAEAENDA"] = dwWS["WALAEAEND"];
                dwABP["ABPSOLLMENGE1"] = QTYMAT * 100;
                dwABP["ABPSOLLMENGE2"] = QTYEND * 100;
                dwABP["ABPSOLLMENGE3"] = 100 * Math.Ceiling(QTYMAT * WARWBRT * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE4"] = 100 * Math.Ceiling(QTYEND * WAFBRTL * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE5"] = 100 * Math.Ceiling(QTYMAT * WARGRLFD * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE6"] = 100 * Math.Ceiling(QTYEND * WAFGRLFD * Convert.ToDouble(0.001));
                dwABP["ABPSOLLMENGE7"] = 100 * Math.Ceiling(QTYEND / (WAFSTDLG * Convert.ToDouble(0.001)));
                dwABP["ABPSOLLMENGE8"] = 100 * Math.Ceiling(QTYMAT / (WARSTDLG * Convert.ToDouble(0.001)));
                #endregion
                dwABP["ABPURAUFKRS"] = ((string)PM["VKAUF"]).Substring(0, 2);
                dwABP["ABPURAUFLFD"] = ((string)PM["VKAUF"]).Substring(2, 6);
                dwABP["ABPURAUFPOS"] = ((string)PM["VKAUF"]).Substring(8, 3);
                dwABP["ABPASAUFKRS"] = ((string)PM["VKAUF"]).Substring(0, 2);
                dwABP["ABPASAUFLFD"] = ((string)PM["VKAUF"]).Substring(2, 6);
                dwABP["ABPASAUFPOS"] = ((string)PM["VKAUF"]).Substring(8, 3);
                dwABP["ABPREAFOLGE"] = ((string)PM["VKAUF"]).Substring(11, 3);
                Insert(dwABP);
                IsABPCreated = true;
                VPR.UpdateICC_PRULES(dwRule, 1);
                PreparePM(dwABP);//
                PM["WAKZ"] = "F";
                PM["WDTYP"] = "Z";
                PM["INMODE"] = INMODE;
                PM["OUTMODE"] = OUTMODE;
                VWD.NEWWDZ(PM);
                VPAR.NABPR(PM);
                VABF.ABFNEW(PM);
                PM["VKAUF"] = dwRule["P4"];
                PM["VKPAUF"] = dwRule["P5"];
                PM["ABAAUFTYP"] = "V";
                VMU.MU2RETURNPROCESS(PM);
                dwData = VABF.TBMain.NewRow();
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABFAUFKRS"] = ((string)PM["VKAUF"]).Substring(0, 2);
                dwData["ABFAUFLFD"] = ((string)PM["VKAUF"]).Substring(2, 6);
                dwData["ABFAUFPOS"] = ((string)PM["VKAUF"]).Substring(8, 3);
                dwData["ABFAFOLGE"] = ((string)PM["VKAUF"]).Substring(11, 3);
                dwData["ABFREAUFKRS"] = dwABP["ABPAUFKRS"];
                dwData["ABFREAUFLFD"] = dwABP["ABPAUFLFD"];
                dwData["ABFREAUFPOS"] = dwABP["ABPAUFPOS"];
                VABF.UABF19(dwData);
                PM["WAKZ"] = "R";
                PM["WDTYP"] = "B";
                PM["MAKE2ORD"] = pm["MAKE2ORD"];
                PM["IsDI"] = false; 
                VWD.NEWWDB(PM);
                Lastmsg = null;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }
        }
    }
}
